package com.wtpaint.leetcode.查找表;

import java.util.*;

public class FrequencySort451 {

    public static String frequencySort(String s) {
        if (s == null) {
            return "";
        }
        char[] c = s.toCharArray();

        Map<Character, Integer> res = new TreeMap<>();
        for (int i = 0; i < c.length; i++) {
            if (res.containsKey(c[i])) {
                res.put(c[i], res.get(c[i]) + 1);
            } else {
                res.put(c[i], 1);
            }
        }

        List<Map.Entry<Character, Integer>> list = new ArrayList<>(res.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Character, Integer> temp : list) {
            for (int i = 0; i < temp.getValue(); i++) {
                sb.append(temp.getKey());
            }
        }
        return sb.toString();

    }


    public static void main(String[] args) {
        System.out.println(frequencySort("cccaaa"));
    }
}
